package cn.wolfcode.service.impl;

import cn.wolfcode.domain.Permission;
import cn.wolfcode.mapper.PermissionMapper;
import cn.wolfcode.qo.QueryObject;
import cn.wolfcode.service.IPermissionService;
import cn.wolfcode.util.RequirePermission;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.ApplicationContext;
import org.springframework.stereotype.Controller;
import org.springframework.stereotype.Service;

import java.lang.reflect.Method;
import java.util.*;

@Service
public class PermissionServiceImpl implements IPermissionService {
    @Autowired
    private PermissionMapper permissionMapper;

    @Override
    public void save(Permission permission) {
        //检查参数合法性
        //假设业务很复杂
        permissionMapper.insert(permission);
    }

    @Override
    public void delete(Long id) {
        permissionMapper.deleteByPrimaryKey(id);
    }

    @Override
    public void update(Permission permission) {
        permissionMapper.updateByPrimaryKey(permission);
    }

    @Override
    public Permission get(Long id) {
        return permissionMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<Permission> listAll() {
        return permissionMapper.selectAll();
    }


    @Override
    public PageInfo<Permission> query(QueryObject qo) {
        /*int count = permissionMapper.selectForCount(qo);
        if (count == 0) {
            return new PageResultBak<>(qo.getCurrentPage(), qo.getPageSize()
                    , Collections.emptyList(), count);
        }
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageResultBak<>(qo.getCurrentPage(), qo.getPageSize(), permissions, count);*/

        //工具的分页插件
        //可以点startPage,再点下载源码,就能看见中文注释
        //业务层控制器要跟着修改
        //开始分页(需要传入当前页,传入每一页显示数量),下一句
        PageHelper.startPage(qo.getCurrentPage(),qo.getPageSize());
        //sql不要写limit,插件会自动在sql语句后面加上limit语句
        List<Permission> permissions = permissionMapper.selectForList(qo);
        return new PageInfo<>(permissions);
    }

    @Autowired
    private ApplicationContext ctx;
    @Override
    public void reload() {
        //6.1查询数据库中所有权限表达式
        List<Permission> permissions = permissionMapper.selectAll();
        //6.2将permissions元素拿出来,存到一个String类型的集合里,便于后面同类型的判断比较
        ArrayList<String> list = new ArrayList<>();
        for(Permission p: permissions){
            list.add(p.getExpression());
        }

        //1.获取所有的controller(数组)
        //把所有的controller中贴了权限注解的方法,转换成权限对象,并保存到数据库中
        //利用spring上下文,获取带有controller注解的所有bean(根据注解)
        Map<String, Object> beans = ctx.getBeansWithAnnotation(Controller.class);
        //拿到所有控制器
        Collection<Object> collections = beans.values();
        //2.遍历获取每一个controller
        for (Object controller : collections){
            //3.获取controller中的方法(数组)
            Method[] methods = controller.getClass().getDeclaredMethods();
            //4.遍历获取每一个方法
            for (Method method:methods){
                //5.判断方法是否有贴自定义的权限注解
                RequirePermission requirePermission = method.getAnnotation(RequirePermission.class);
                if (requirePermission==null){
                    //没贴跳过
                    continue;
                }
                //有贴
                //6.从该注解中获取name和expression的属性值,封装成一个Permission权限对象
                String name = requirePermission.name();
                String expression = requirePermission.expression();

                //去重
                //判断该表达式是否已经存在数据库中,如果存在则不插入
                if(!list.contains(expression)){
                    Permission permission = new Permission();
                    permission.setName(name);
                    permission.setExpression(expression);
                    //7.把该权限插入到数据库
                    permissionMapper.insert(permission);
                }

            }
        }
    }

    @Override
    public List<String> selectByEmpId(Long id) {
        return permissionMapper.selectByEmpId(id);
    }


}
